package org.cloud.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.cloud.domain.form.LoginBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {
	
	@RequestMapping(value = "index")
	public String index(){
		return "index";
	}
	
	@RequestMapping(value = "login",method = RequestMethod.GET)
	private String toLogin(ModelMap modelMap){
		modelMap.addAttribute(new LoginBean());
		return "login";
	}
	
	@RequestMapping(value = "login",method = RequestMethod.POST)
	public String doLogin(LoginBean loginBean,BindingResult bindingResult){
		UsernamePasswordToken token = new UsernamePasswordToken(loginBean.getUsername(),loginBean.getPassword());
		Subject subject = SecurityUtils.getSubject();
		try{
			subject.login(token);
			return "index";
		}catch(Exception e){
			
		}
		return "login";
	}
	
	private static Log log = LogFactory.getLog(MainController.class);
	
	public static void main(String[] args) {
		log.error("hello word!");
		log.info("hello word!");
	}

}
